package PDFd;


import gen.R;
import PDFd.Instance.InstanceManager;
import PDFd.Instance.PersistanceException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * Simple activity that passes button state information from the document editor off to the CanvasView
 * @author mckaym
 *
 */
public class PDFdActivity extends Activity {
	public void onPause() {
		doPersist();
	}
	/**
	 * The canvas view the button state is passed to
	 */
	private CanvasView c;
	/**
	 * Disables all the buttons but Done and Home, then tells the CanvasView to go to a specific state
	 * @param b The state to go to.
	 */
	public void inProcess(CanvasViewButtonStates b) {
		//Hide all da buttons
    	((Button)findViewById(R.id.draw)).setVisibility(Button.GONE);
//    	((Button)findViewById(R.id.text)).setVisibility(Button.GONE);
    	((Button)findViewById(R.id.layer)).setVisibility(Button.GONE);
    	((Button)findViewById(R.id.done)).setVisibility(Button.VISIBLE);
    	c.userStateChange(CanvasViewButtonStates.View);
    	c.userStateChange(b);
    	
	}
	/**
	 * Reenables all the buttons, goes back to the default view.
	 */
	public void doneWithProcess() {
    	((Button)findViewById(R.id.draw)).setVisibility(Button.VISIBLE);
//    	((Button)findViewById(R.id.text)).setVisibility(Button.VISIBLE);
    	((Button)findViewById(R.id.layer)).setVisibility(Button.VISIBLE);
    	((Button)findViewById(R.id.done)).setVisibility(Button.GONE);
    	c.userStateChange(CanvasViewButtonStates.View);
	}
	/**
	 * Setup the button handlers
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		c = ((CanvasView)findViewById(R.id.canvasView));
    	c.userStateChange(CanvasViewButtonStates.View);
    	c.activityContext = this;
    	//Our buttans!
    	final Button draw = ((Button)findViewById(R.id.draw));
//    	final Button text = ((Button)findViewById(R.id.text));
    	final Button layer = ((Button)findViewById(R.id.layer));
    	final Button done = ((Button)findViewById(R.id.done));
    	final Button home = ((Button)findViewById(R.id.home));
    	
    	draw.setOnClickListener(new OnClickListener() {
    	    public void onClick(View v) {
    	    	inProcess(CanvasViewButtonStates.Draw);
    	    }
    	  });
//    	text.setOnClickListener(new OnClickListener() {
//    	    public void onClick(View v) {
//    	    	inProcess(CanvasViewButtonStates.Text);
//    	    }
//    	  });
    	
    	layer.setOnClickListener(new OnClickListener() {
    	    public void onClick(View v) {
    	    	inProcess(CanvasViewButtonStates.Layer);
    	    }
    	  });
    	done.setOnClickListener(new OnClickListener() {
    		public void onClick(View v) {
    			doneWithProcess();
    		}
    	});
    	home.setOnClickListener(new OnClickListener() {
    		@SuppressWarnings("static-access")
			public void onClick(View v) {
    			doPersist();
    		}
    	});
	}
	private void doPersist() {
		try {
			doneWithProcess();
			InstanceManager.getInstanceManager().instances.remove(c.getPDFdInstance());
			InstanceManager.getInstanceManager().instances.add(c.getPDFdInstance());
			InstanceManager.getInstanceManager().persist();
            PDFdActivity.this.finish();
		} catch (PersistanceException e) {
		}
	}
}